﻿using Bandit.Helpers;
using Bandit.UI;
using ETC.DLEDLightingSystemPlus.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ETC.DLEDLightingSystemPlus.Windows
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : BanditWindow
    {

        public ViewModels.MainViewModel ViewModel { get { return App.MainViewModel; } }

        public LoginWindow()
        {
            InitializeComponent();

            this.DataContext = ViewModel;

        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Application.Current.Shutdown();
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if (cbStationConfigName.Text == "")
            {
                //MessageBox.Show(App.GetString("PleaseSelectConfigFile"));
                this.ShowPromptWindow(App.GetString("PleaseSelectConfigFile"), PromptType.Red);
            }
            else
            {

                if(File.Exists(this.GetBaseDirectory() + "TestItemConfig.json"))
                {
                    App.StationConfigName = System.IO.Path.GetFileNameWithoutExtension(cbStationConfigName.Text);

                    ConfigHelper.XmlFileSerialize<Setting>(this.ViewModel.Setting, Setting.SavePath);

                    (new MainWindow()).Show(); // 显示主窗口;

                    Close();

                }else
                {
                    //MessageBox.Show("缺少TestItemConfig.json文件，请联系工程人员","异常提示",MessageBoxButton.OK,MessageBoxImage.Error);
                    this.ShowPromptWindow("缺少TestItemConfig.json文件，请联系工程人员",PromptType.Red,5);
                }
            }
        }


        //新建一个专案 xml。
        private void btnNewAdd_Click(object sender, RoutedEventArgs e)
        {
            string fileName = string.Empty;

            Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();

            sfd.InitialDirectory = App.GetPathOfConfig();
            sfd.FileName = App.GetFilePathOfTestStationConfig();
            sfd.Filter = "Config|*" + App.TestStationFileExtension;
            if (sfd.ShowDialog().Value)
            {
                fileName = sfd.FileName;

                App.StationConfigName = System.IO.Path.GetFileNameWithoutExtension(fileName);

                List<TestStation> list = new List<TestStation>();

                list.Add(new TestStation(new Models.StationInfo("Name1","COM999",1)));

                var stationInfos = list.Select(s => s.StationInfo).OrderBy(s => s.Index).ToArray();

                ConfigHelper.XmlFileSerialize(stationInfos, App.GetFilePathOfTestStationConfig());

                ViewModel.Setting.LoadConfigNames();

                cbStationConfigName.Items.Refresh();

                ViewModel.Setting.ConfigName = App.StationConfigName + ".config";
            }
            else
            {
                fileName = sfd.FileName;
            }

        }
    }
}
